package sys.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.aspectj.bridge.MessageWriter;

import freemarker.template.utility.NullArgumentException;

public class EntityUtil {

	public static String getKeyName(Class clzz) {
		Field[] fields = clzz.getDeclaredFields();
		Method[] methods = clzz.getDeclaredMethods();
		for (Method method : methods) {
		
			Annotation[] annotation = method.getAnnotations();
			for (Annotation annotation2 : annotation) {
				if (annotation2.annotationType().getSimpleName().equals("Id")) {

					String methName = method.getName();
					methName = methName.substring(3, methName.length());
					methName = methName.substring(0, 1).toLowerCase() + methName.substring(1);
					return methName;
				}

			}
		}

		for (Field field : fields) {
			String fileName = field.getName();
		
			// Annotation annotation =field.getAnnotation(Id.class);
			Annotation[] annotation = field.getAnnotations();
			for (Annotation annotation2 : annotation) {
				if (annotation2.annotationType().getSimpleName().equals("Id")) {

					return fileName;
				}

			}

		}
		return null;
	}
	public static <T> Object getValue(T entiy,String fieldName,Class<? extends Object> class1) 
	{
		if (entiy==null) {
			return null;
		}
		Field[] fields = class1.getDeclaredFields();
		for (Field field : fields) {
			if (field.getName().equals(fieldName)) {
				try {
					field.setAccessible(true) ; 
					return field.get(entiy);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
			}
		}
		return null;
		
	}
}
